package com.puzzlebazar.client.hashiwokakero;

import com.puzzlebazar.client.puzzle.model.Move;
import com.puzzlebazar.client.puzzle.model.MoveImpl;
import com.puzzlebazar.client.puzzle.model.PuzzleModel;
import com.puzzlebazar.client.util.Vec2i;

public abstract class MoveBridge extends MoveImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7220934434431718701L;
	
	private Vec2i connections[] = new Vec2i[2];
	private int   value;
	
	/**
	 * Only for serialization
	 */
	protected MoveBridge() {
		super();
	}
	
	
	/**
	 * A move that contains all the information required to create or delete a bridge
	 * 
	 * @param connection1 The location of the first end of the bridge
	 * @param connection2 The location of the second end of the bridge
	 * @param value The value of the bridge
	 */
	public MoveBridge(Vec2i connection1, Vec2i connection2, int value) {
		super(-1);
		this.connections[0] = connection1;
		this.connections[1] = connection2;
		this.value = value;
	}

	/**
	 * @param i The end of the bridge to access (0 or 1)
	 * @return The position to which this end of the bridge is connected
	 */
	public Vec2i getLoc( int i ) throws Exception {
		if( i < 0 || i >= 2 ) throw new Exception( "Invalid connection end" );		
		return connections[i];
	}
	
	/**
	 * @return The value of the added or removed island
	 */
	public int getValue() {
		return value;
	}		

	/**
	 * Create the bridge described in this move
	 * 
	 * @param model The model into which to create the bridge
     * @return A Move List that can be used for a more efficient call to notifyObserver()
	 * @throws Exception
	 */
	protected Move createBridge(PuzzleModel model) throws Exception {
		assert model instanceof HashiwokakeroModel;
		return ((HashiwokakeroModel)model).addBridge( connections[0], connections[1], value );
	}
	
	/**
	 * Deletes the bridge described in this move
	 * 
	 * @param model The model from which to delete the bridge
     * @return A Move List that can be used for a more efficient call to notifyObserver()
	 * @throws Exception
	 */
	protected Move deleteBridge(PuzzleModel model) throws Exception {
		assert model instanceof HashiwokakeroModel;
		return ((HashiwokakeroModel)model).removeBridge( connections[0], connections[1] );
	}	
	
}
